package com.atguigu.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CompletableFutureDemo1
 * Package: com.atguigu.ssyx
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-04
 * Time: 13:11
 */

//CompletableFuture 计算完成时候回调方法 whenComplete
public class CompletableFutureDemo2 {

    public static void main(String[] args) {

        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);
        System.out.println("main begin ...");

        //创建异步对象
        CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程: " + Thread.currentThread().getName());
            int value = 1024/0;
            System.out.println("value:" + value);
            return value;
            //当这个任务完成之后调用whenComplete方法
        }, service).whenComplete((rs,exception)->{
            System.out.println("whenComplete: " + rs);
            System.out.println("exception: " + exception);
        });


        System.out.println("main over ...");


    }

}
